/*
 * 作者: 张帆
 * 时间: 2017-04-17 
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */	
define(function(require) {
	
	require("jquery");

	var c = require('app/utils/CommonUtils');
	var APIUtils = require('app/utils/APIUtils');
	var appInfo = require('app/AppInfo');
	var hc = require('app/utils/HttpClientUtils');
	var action_search = require('app/action/SearchIconFrmAction');
	var EventUtils=require('app/utils/EventUtils');
	var param = api.pageParam;
	var body={};
	body.record=[];
	if(param.formList){
		var list=param.formList;
		var listDom;
		list.forEach(function(item,key){
			listDom=
					'<li class="aui-list-item">'+
			           '<div class="aui-list-item-inner">'+
			                '<div class="aui-list-item-label">'+
			                    item.name+
			                '</div>'+
			                '<div class="aui-list-item-input">'+
			                    '<input type="text">'+
			                '</div>'+
			            '</div>'+
			        '</li>'
			$(".enrolldetails").append(listDom);
			$(".enrolldetails li").eq(key).find('input').attr("key",item.fieldid);
			
			if(param.type=='change'){
				$(".enrolldetails li").eq(key).find('input').val(param.formData['field'+item.ordernum].fieldvalue);
			}
		})
		var btnDom=
			    '<li id="list-btn" class="aui-list-item">'+
			        '<div class="aui-list-item-inner">'+
			        	'<div id="Preservation" style="margin-left: 120px" class="aui-btn aui-btn-primary">保存</div>'+
			            '<div id="delete" style="margin-right: 120px" class="aui-btn aui-btn-danger">删除</div>'+
			        '</div>'+
	        	'</li>'
		$(".btn-list").append(btnDom);
	}
	$("#back").click(function(){
		api.closeWin({
	    });
	});
	$(".enrolldetails li").each(function(key){
		body.record.push({
			'fieldid':$(this).find("input").attr("key"),
			'fieldvalue':$(".enrolldetails li").eq(key).find("input").val()
		})
		$(".enrolldetails li").eq(key).find("input").change(function(){
			body.record[key].fieldvalue=$(this).val()
		})
	})
	$("#Preservation").click(function(){
		if(param.type=='add'){
			var url=appInfo.serverbase+'/luyan/form/'+param.formid+'/record/create';
			hc.post(url, body, function(resp) {
				api.closeWin({
			    });
			    EventUtils.send('EnrollWin','refresh_enroll_list');
			})
		}else if(param.type=='change'){
			var url=appInfo.serverbase+'/luyan/form/'+param.formid+'/record/'+param.recordid+'/update';
			hc.post(url, body, function(resp) {
				console.log(resp)
				api.closeWin({
			    });
			    EventUtils.send('EnrollWin','refresh_enroll_list');
			})
		}
	});
	$("#delete").click(function(){
		api.confirm({
			title: "温馨提示",
			msg: '是否删除？',
		    buttons: ['取消', '确定']
		},function(ret,err){
		    if(ret.buttonIndex == 2){
		        var url=appInfo.serverbase+'/luyan/form/'+param.formid+'/record/'+param.recordid+'/delete';
		        var body={};
		        hc.post(url,body,function(resp){
		        	if(resp.status==200){
		 		        api.closeWin({
        				});       	
						EventUtils.send('EnrollWin','refresh_enroll_list');
		        	}else{
		        	
		        	}
		        })
		    }else{
				alert('删除失败')
		    }
		})
	})
	return;
});